<?php
defined('SYSPATH') or die('No direct script access.') ;
/**
  * @author Y.N
 *  2011/5/9
 */
class Cli_Shops extends CLI
{
	public function main()
	{
		$shops = array(
			'25057' => array(23071, 23349, 23362, 23363, 23364, 23497, 23388, 23483,
				23484, 23123, 23367, 23389, 23390, 23391, 23431, 23217, 23481, 23488,
				23498, 23351, 23354, 23955, 23957, 23952, 24341, 24393, 24848, 24908,
				24911, 24932, 24940, 24973, 24975, 24976, 24981, 24983, 24984, 24985,
				25025, 25026, 25030, 25044
			),
		);
		foreach ($shops as $k => $value)
		{
			foreach ($value as $v)
			{
				$goods = DB::select('*')
					->from('goods')
					->where('shop_id', '=', (int) $v)
					->where('deleted', '=', 0)
					->fetch_all();
				foreach ($goods as $g)
				{
					/* 插入goods商品表 */
					$goodsInfo = array(
						'deleted' => $g['deleted'],
						'on_sale' => $g['on_sale'],
						'sales' => $g['sales'],
						'retail_price' => $g['retail_price'],
						'expire_time' => $g['days'] * 24 * 3600 + time(),
						'shop_id' => (int) $k,
						'product_id' => $g['product_id'],
						'cid' => $g['cid'],
						'props' => $g['props'],
						'props_str' => $g['props_str'],
						'input_pids' => $g['input_pids'],
						'input_str' => $g['input_str'],
						'goods_title' => $g['goods_title'],
						'description' => $g['description'],
						'image_md5' => $g['image_md5'],
						'max_price' => $g['max_price'],
						'on_sale' => $g['on_sale'],
						'freight_id' => $g['freight_id'],
						'days' => $g['days'],
						'updated' => time(),
						'invoice' => $g['invoice'],
						'sort_order' => $g['sort_order'],
						'is_top' => $g['is_top'],
						'stock' => $g['stock'],
					);
					$last = DB::insert('goods', array_keys($goodsInfo))
						->values(array_values($goodsInfo))
						->execute();
					$lastId = current($last); // 最后插入的商品ID

					// 插入扩展表
					$description = DB::select('description')
						->from('goods_fields')
						->where('goods_id', '=', $g['goods_id'])
						->fetch_one();

					$goods_fields = array(
						'goods_id' => $lastId,
						'description' => $description,
					);
					DB::replace('goods_fields', $goods_fields)->execute();

					/* 插入goods_images图片表 */
					$goods_images = DB::select('*')
						->from('goods_images')
						->where('goods_id', '=', $g['goods_id'])
						->fetch_all();

					foreach($goods_images as $goodimg)
					{
						$imagesInfo = array(
							'goods_id' => $lastId,
							'image_md5' => $goodimg['image_md5'],
							'product_id' => $goodimg['product_id'],
							'sort_order' => $goodimg['sort_order'],
							'is_zoom' => $goodimg['is_zoom'],
						);
						DB::insert('goods_images', array_keys($imagesInfo))
							->values(array_values($imagesInfo))
							->execute();
					}

					/* 插入goods_props属性表 */
					$goods_props = DB::select('*')
						->from('goods_props')
						->where('goods_id', '=', $g['goods_id'])
						->fetch_all();
					foreach($goods_props as $props)
					{
						$props_values = array(
							'goods_id' => $lastId,
							'props' => $props['props'],
						);
						DB::replace('goods_props',$props_values)->execute();
					}

					CLI::output(">");
				}
			}
		}
		CLI::output("\n\n店铺商品数据复制成功！");
	}
}